home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 58271 / 58271.xpi / chrome / content / dlScheduler.js next >
Text File  |  2010-02-10  |  11KB  |  266 lines

  1.  
  2. if(!tim_matthews) var tim_matthews={};
  3. if(!tim_matthews.downloadScheduler) tim_matthews.downloadScheduler={};
  4. if(!tim_matthews.downloadScheduler.dlScheduler_js) tim_matthews.downloadScheduler.dlScheduler_js = {};
  5.  
  6. tim_matthews.downloadScheduler.dlScheduler_js = {
  7.  
  8.   hmFromTimeString: function(timeString) {
  9.       var s = timeString.split(":");
  10.       var obj = {};
  11.       obj.hours = parseInt(s[0]);
  12.       obj.mins = parseInt(s[1]);
  13.       
  14.       return obj;
  15.   },
  16.  
  17.  
  18.   timer: {
  19.     startTimer: null,
  20.     finishTimer: null,
  21.     cancel: function() {
  22.       if(this.startTimer) {
  23.           this.startTimer.cancel();
  24.           this.startTimer = null;
  25.       }
  26.       if(this.finishTimer) {
  27.           this.finishTimer.cancel();
  28.           this.finishTimer = null;
  29.       }
  30.     },
  31.     setupTimer: function() {
  32.         this.cancel();
  33.  
  34.         var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("dlScheduler.");
  35.         var startHM = tim_matthews.downloadScheduler.dlScheduler_js.hmFromTimeString(prefs.getCharPref("startTime"));
  36.         var finishHM = tim_matthews.downloadScheduler.dlScheduler_js.hmFromTimeString(prefs.getCharPref("finishTime"));
  37.  
  38.         /* Set the next start time from today */
  39.         
  40.         var now = new Date();
  41.  
  42.         var startDate = new Date();
  43.         startDate.setHours(startHM.hours);
  44.         startDate.setMinutes(startHM.mins);
  45.         startDate.setSeconds(0);
  46.         /* If we've missed today's schedule, set time at tommorows date */
  47.         if(startDate.getTime() < now.getTime())
  48.             startDate.setTime(startDate.getTime() + 86400000);
  49.         
  50.         var finDate = new Date();
  51.         finDate.setHours(finishHM.hours);
  52.         finDate.setMinutes(finishHM.mins);
  53.         finDate.setSeconds(0);
  54.         if(finDate.getTime() < startDate.getTime())
  55.             finDate.setTime(finDate.getTime() + 86400000);
  56.  
  57.         var msStart = startDate.getTime() - now.getTime();
  58.         var msFin = finDate.getTime() - now.getTime();
  59.  
  60.         this.startTimer = Components.classes["@mozilla.org/timer;1"].createInstance(Components.interfaces.nsITimer);
  61.         this.startTimer.initWithCallback({ notify: function(timerr) { tim_matthews.downloadScheduler.dlScheduler_js.startDownloads(); } }, msStart, Components.interfaces.nsITimer.TYPE_ONE_SHOT);
  62.  
  63.         this.finishTimer = Components.classes["@mozilla.org/timer;1"].createInstance(Components.interfaces.nsITimer);
  64.         this.finishTimer.initWithCallback({ notify: function(timerr) { tim_matthews.downloadScheduler.dlScheduler_js.pauseDownloads(); } }, msFin, Components.interfaces.nsITimer.TYPE_ONE_SHOT);
  65.     },
  66.     observe: function(subject, topic, data) {
  67.         if (topic != "nsPref:changed")
  68.             return;
  69.         this.setupTimer();
  70.     }
  71.   },
  72.  
  73.   thumbnailsShowHideItems: function(event) {
  74.     var contextSched = document.getElementById("tim_matthews.downloadScheduler.context-schedulelink");
  75.     var contextSep = document.getElementById("tim_matthews.downloadScheduler.context-separator1");
  76.     contextSched.hidden = document.getElementById("context-savelink").hidden;
  77.     contextSep.hidden = contextSched.hidden;
  78.   },
  79.  
  80.   init: function() {
  81.       try {
  82.           var contextMenu = document.getElementById("contentAreaContextMenu");
  83.  
  84.           if (contextMenu)
  85.               contextMenu.addEventListener("popupshowing", tim_matthews.downloadScheduler.dlScheduler_js.thumbnailsShowHideItems, false);
  86.  
  87.           var downloadArray = [];
  88.           Application.storage.set("tim_matthews.downloadScheduler.downloadArray",  downloadArray);
  89.  
  90.           tim_matthews.downloadScheduler.dlScheduler_js.timer.setupTimer();
  91.  
  92.           tim_matthews.downloadScheduler.dlScheduler_js.timer.prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("dlScheduler.");
  93.           tim_matthews.downloadScheduler.dlScheduler_js.timer.prefs.QueryInterface(Components.interfaces.nsIPrefBranch2);
  94.           tim_matthews.downloadScheduler.dlScheduler_js.timer.prefs.addObserver("", tim_matthews.downloadScheduler.dlScheduler_js.timer, false);
  95.           
  96.           Application.storage.set("tim_matthews.downloadScheduler.timer", tim_matthews.downloadScheduler.dlScheduler_js.timer);
  97.  
  98.       } catch (e) {
  99.           alert(e);
  100.       }
  101.   },
  102.  
  103.   showScheduler: function() {
  104.       window.open("chrome://dlScheduler/content/schedWin.xul", "tim_matthews.downloadScheduler.schedWin", "chrome, width=360, height=220" );
  105.   },
  106.  
  107.   startDownloads: function() {
  108.       try {
  109.           var downloadArray = Application.storage.get("tim_matthews.downloadScheduler.downloadArray",  null);
  110.           if(downloadArray.length==0)
  111.               return;
  112.  
  113.           var dm = Components.classes["@mozilla.org/download-manager;1"].getService(Components.interfaces.nsIDownloadManager);
  114.           var dlmgrWindow = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator).getMostRecentWindow("Download:Manager");
  115.           var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  116.           if (dlmgrWindow)
  117.           {
  118.               if(prefs.getBoolPref("browser.download.manager.focusWhenStarting"))
  119.                   dlmgrWindow.focus();
  120.           }
  121.           else
  122.           {            
  123.               if(prefs.getBoolPref("browser.download.manager.showWhenStarting"))
  124.                   openDialog("chrome://mozapps/content/downloads/downloads.xul", "Download:Manager", "chrome,centerscreen", null);
  125.           }
  126.  
  127.           var activeDl = dm.activeDownloads;
  128.           while(activeDl.hasMoreElements())
  129.           {
  130.               var dl = activeDl.getNext().QueryInterface(Components.interfaces.nsIDownload);
  131.               if(dl.state==4)
  132.                   dm.resumeDownload(dl.id);
  133.           }
  134.    
  135.           for (var i=0; i<downloadArray.length; i++)
  136.           {
  137.               var scheduleSlot = downloadArray[i];
  138.               if((scheduleSlot == undefined) || (scheduleSlot==null))
  139.                   continue;
  140.  
  141.               //alert(scheduleSlot.targetURI);
  142.               //alert(scheduleSlot.targetFile.path);
  143.  
  144.               var obj_Persist = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"].createInstance(Components.interfaces.nsIWebBrowserPersist);
  145.               const nsIWBP = Components.interfaces.nsIWebBrowserPersist;
  146.               const flags = nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
  147.               obj_Persist.persistFlags = flags | nsIWBP.PERSIST_FLAGS_FROM_CACHE;
  148.  
  149.               var download = dm.addDownload(0, scheduleSlot.sourceURI, scheduleSlot.targetURI,  null, null, null, null, obj_Persist);
  150.  
  151.               obj_Persist.progressListener = download;
  152.  
  153.               obj_Persist.saveURI(scheduleSlot.sourceURI, null, null, null, null, scheduleSlot.targetFile);
  154.           }
  155.  
  156.           downloadArray = [];
  157.           Application.storage.set("tim_matthews.downloadScheduler.downloadArray", downloadArray);
  158.  
  159.           var scheduleWindow = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator).getMostRecentWindow("tim_matthews.downloadScheduler.schedWindow");
  160.           if(scheduleWindow)
  161.               scheduleWindow.tim_matthews.downloadScheduler.schedWin_js.refreshList();
  162.  
  163.       } catch (e) {
  164.           alert(e);
  165.       }
  166.       
  167.   },
  168.  
  169.   pauseDownloads: function() {
  170.       try {
  171.           var dm = Components.classes["@mozilla.org/download-manager;1"].getService(Components.interfaces.nsIDownloadManager);
  172.           var activeDl = dm.activeDownloads;
  173.           while(activeDl.hasMoreElements())
  174.           {
  175.               var dl = activeDl.getNext().QueryInterface(Components.interfaces.nsIDownload);
  176.               if(dl.state==0)
  177.                   dm.pauseDownload(dl.id);
  178.           }
  179.  
  180.           tim_matthews.downloadScheduler.dlScheduler_js.timer.setupTimer();
  181.  
  182.       } catch (e) {
  183.           alert(e);
  184.       }
  185.   },
  186.  
  187.   parseURL: function(url) {
  188.       var fileName = {};
  189.       fileName.file = "";
  190.       fileName.ext = "";
  191.       var slash = url.split("/");
  192.       if(slash.length > 0)
  193.       {
  194.           var file = slash[slash.length-1];
  195.           if( (file.length > 0) && (file.indexOf("?") == -1) )
  196.           {
  197.               var dot = file.split(".");
  198.               if(dot.length > 0)
  199.               {
  200.                   var ext = dot[dot.length-1];
  201.                   fileName.file = file;
  202.                   fileName.ext = ext;
  203.               }
  204.           }
  205.       }
  206.       return fileName;
  207.   },
  208.  
  209.   scheduleDownload: function() {
  210.     try {
  211.           var nsIFilePicker = Components.interfaces.nsIFilePicker;
  212.           var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
  213.  
  214.           fp.init(window, "Enter name of file for scheduled download...", nsIFilePicker.modeSave);
  215.           
  216.           var fileName = tim_matthews.downloadScheduler.dlScheduler_js.parseURL(gContextMenu.linkURL);
  217.           fp.defaultString = fileName.file;
  218.           fp.defaultExtension = fileName.ext;
  219.           if(fileName.ext.length > 0)
  220.           {
  221.               fp.appendFilter(fileName.ext, "*." + fileName.ext);
  222.           }
  223.           fp.appendFilter("All files (*.*)", "*.*");
  224.  
  225.           if(fp.show() == 1)
  226.               return;
  227.  
  228.           var sourceURI = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService).newURI(gContextMenu.linkURL, null, null);
  229.  
  230.           var targetFile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
  231.           
  232.           var myURL = fp.fileURL.QueryInterface(Components.interfaces.nsIURL);
  233.           //alert(fp.file.path);
  234.           targetFile.initWithPath(fp.file.path);
  235.  
  236.           var newFile = false;
  237.  
  238.       if(!targetFile.exists())
  239.           {
  240.         targetFile.create(0x00,0644);
  241.               newFile = true;
  242.           }
  243.  
  244.           var downloadArray = Application.storage.get("tim_matthews.downloadScheduler.downloadArray",  null);
  245.           var scheduleSlot = {};
  246.           scheduleSlot.sourceURI = sourceURI;
  247.           scheduleSlot.targetURI = fp.fileURL;
  248.           scheduleSlot.targetFile = targetFile;
  249.           scheduleSlot.newFile = newFile;
  250.           downloadArray.push(scheduleSlot);
  251.  
  252.           var scheduleWindow = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator).getMostRecentWindow("tim_matthews.downloadScheduler.schedWindow");
  253.           if(scheduleWindow)
  254.               scheduleWindow.tim_matthews.downloadScheduler.schedWin_js.refreshList();
  255.  
  256.     } catch (e) {
  257.       alert(e);
  258.     }
  259.   }
  260.  
  261. };
  262.  
  263. window.addEventListener("load", tim_matthews.downloadScheduler.dlScheduler_js.init, false);
  264.  
  265.  
  266.